<template>
  <div class="app-container">

    <el-form ref="form" :model="form" label-width="80px" inline>
      <el-form-item  label="姓名">
        <el-input style="width: 80%;"  v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="性别" >
        <el-input style="width: 80%;"  v-model="form.gender"></el-input>
      </el-form-item>
<!--      <el-form-item  label="入住时间">-->
<!--        <el-input style="width: 80%;"  v-model="form.useRoom"></el-input>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" @click="onSubmit(form)">查询</el-button>
        <el-button  @click="onReset(form)">重置</el-button>
        <el-button type="success" @click="showDia('', 'add')">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        type="index"
        width="30">
      </el-table-column>
      <el-table-column align="center" label="姓名" width="95">
        <template slot-scope="scope">
          <!--          {{ scope.$index }}-->
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="性别" width="100"  align="center">
        <template slot-scope="scope">
          {{ scope.row.gender }}
        </template>
      </el-table-column>
      <el-table-column label="生日" width="150" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.birthday }}</span>
        </template>
      </el-table-column>
      <el-table-column label="病史" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.disHistory }}
        </template>
      </el-table-column>
      <el-table-column label="房间号" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.useRoom }}
        </template>
      </el-table-column>
      <el-table-column label="责任人" width="100" align="center">
        <template slot-scope="scope">
          {{ scope.row.manager }}
        </template>
      </el-table-column>
      <el-table-column label="入住时间" width="160" align="center">
        <template slot-scope="scope">
          {{ scope.row.inDatetime }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="showDia(scope.row.id, 'look')">查看</el-button>
          <el-button
            size="mini"
            @click="showDia(scope.row.id, 'edit')">编辑</el-button>
          <template>
            <el-popconfirm title="确定删除吗？" @onConfirm="handleDelete(scope.row.id)">
              <el-button size="mini" type="danger" slot="reference" >删除</el-button>
            </el-popconfirm>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="margin-top: 40px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="100"
        layout="prev, pager, next, jumper"
        :total="1000">
      </el-pagination>
    </div>
    <el-dialog title="详情" :visible.sync="dialogFormVisible">
      <el-form ref="staffDetail"  label-width="80px" :disabled="diaLook" inline>
        <el-form-item  label="姓名">
          <el-input  style="width: 80%;"  v-model="staffDetail.name"></el-input>
        </el-form-item>
        <el-form-item label="性别" >
          <el-input  style="width: 80%;"  v-model="staffDetail.gender"></el-input>
        </el-form-item>
        <el-form-item  label="生日">
          <el-date-picker
            v-model="staffDetail.birthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="病史">
          <el-input style="width: 80%;"  v-model="staffDetail.disHistory"></el-input>
        </el-form-item>
        <el-form-item label="使用房间">
          <el-input style="width: 80%;"  v-model="staffDetail.useRoom"></el-input>
        </el-form-item>
        <el-form-item label="责任人">
          <el-input style="width: 80%;"  v-model="staffDetail.manager"></el-input>
        </el-form-item>
        <el-form-item label="入住时间">
          <el-date-picker
            v-model="staffDetail.inDatetime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="onCancle">取 消</el-button>
        <el-button type="primary" @click="onUpdate(staffDetail)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getStaffList, getStaffById, updateStaff, deleteStaffById, addStaff } from '@/api/staff'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: false,
      form: {},
      staffDetail: {
        // 'birthday': ''
      },
      currentPage: 1,
      dialogFormVisible: false,
      diaLook: true,
      operate: ''
    }
  },
  created() {
    this.fetchData(this.form)
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.form.pageNum = val
      getStaffList(this.form).then(res => {
        this.list = res.data.rows
      })
    },
    handleEdit(row) {
    },
    showDia(id, operate) {
      this.operate = operate
      if (operate === 'look') {
        getStaffById(id).then(res => {
          this.staffDetail = res.data
          this.diaLook = true
        })
      } else if (operate === 'edit') {
        getStaffById(id).then(res => {
          this.staffDetail = res.data
          this.diaLook = false
        })
      } else if (operate === 'add') {
        this.staffDetail = {
          'birthday': ''
        }
        this.diaLook = false
      }
      this.dialogFormVisible = true
    },
    handleDelete(id) {
      deleteStaffById(id).then(res => {
        this.onSubmit(this.form)
      })
    },
    onSubmit(param) {
      getStaffList(param).then(res => {
        this.list = res.data.rows
      })
    },
    onReset(form) {
      this.form = {}
    },
    onCancle() {
      this.dialogFormVisible = false
      this.staffDetail = {
        'birthday': ''
      }
    },
    onUpdate(data) {
      if (this.diaLook === true) {
        this.dialogFormVisible = false
        this.staffDetail = {
          'birthday': ''
        }
        return
      }
      if (this.operate === 'add') {
        addStaff(data).then(res => {
          this.dialogFormVisible = false
          this.onSubmit(this.form)
          this.equipmentDetail = {}
        })
      } else if (this.operate === 'edit') {
        updateStaff(data).then(res => {
          this.dialogFormVisible = false
          this.onSubmit(this.form)
          this.staffDetail = {
            'birthday': ''
          }
        })
      }
      this.operate = ''
    },
    fetchData(param) {
      getStaffList(param).then(res => {
        this.list = res.data.rows
      })
    }
  }
}
</script>
